# -*- coding: utf-8 -*-
"""
@Time    : 2021/10/9 17:34
@Author  : Leibuyun
@File    : 1.13 通过关键字排序一个字典序列.py
"""

from operator import itemgetter

if __name__ == '__main__':
    rows = [
        {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
        {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
        {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
        {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
    ]
    rows_by_fname = sorted(rows, key=itemgetter('fname'))  # 根据fname字典序由小到大排序
    # 先根据lname排序, 相同则根据fname排序
    # rows_by_lfname = sorted(rows, key=lambda r: (r['lname'], r['fname'])) 也可以用lambda表达式代替
    rows_by_lname_fname = sorted(rows, key=itemgetter('lname', 'fname'))
    print(rows_by_fname)
    print('=' * 20)
    print(rows_by_lname_fname)
